package function;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import job.NewCarBean;
import org.apache.flink.api.common.functions.MapFunction;

import java.text.SimpleDateFormat;

public class CarViewBeanMapFuction implements MapFunction<String, NewCarBean> {

    @Override
    public NewCarBean map(String s) throws Exception {
        //解析JSON
        JSONObject jsonObject = JSON.parseObject(s);
        String event_type = jsonObject.getString("event_type");
        String province = jsonObject.getString("province");
        double motor_temp = jsonObject.getDoubleValue("motor_temp");
        String vin = jsonObject.getString("vin");
        double battery_temp = jsonObject.getDoubleValue("battery_temp");
        Long timestamp = jsonObject.getLong("timestamp");
        JSONObject eventProperties = jsonObject.getJSONObject("event_properties");
        double battery_power = eventProperties.getDoubleValue("battery_power");
        String charging_status = eventProperties.getString("charging_status");
        String start_status = eventProperties.getString("start_status");
        double speed = eventProperties.getDoubleValue("speed");
        double mileage = eventProperties.getDoubleValue("mileage");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateTime = sdf.format(timestamp);
        return new NewCarBean(
                event_type
                ,province
                ,motor_temp
                ,vin
                ,battery_temp
                ,timestamp
                ,battery_power
                ,charging_status
                ,start_status
                ,speed
                ,mileage
                ,dateTime
        );
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
